package com.situ.mall.admin.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.situ.mall.common.entity.Category;
import com.situ.mall.common.vo.TableVO;
import com.situ.mall.common.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lwj
 * @since 2023-09-11
 */
@Controller
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    @GetMapping("/page")
    public String page(){
        return "category";
    }

    @GetMapping("/tree")
    @ResponseBody
    public TableVO tree(){
        return  categoryService.tree();
//        return categoryService.tree();
    }

    @PostMapping("/save")
    @ResponseBody
    public void save(Category category){
        categoryService.saveOrUpdate(category);
    }

    @PostMapping("/delete/{id}")
    @ResponseBody
    public void delete(@PathVariable Integer id){
        categoryService.delete(id);
    }


    @GetMapping("/info/{id}")
    @ResponseBody
    public Category info(@PathVariable Integer id){
       return categoryService.getById(id);
    }


    //获取一级 二级下拉框的值
    @GetMapping("/children")
    @ResponseBody
    public List<Category> list2(Integer id){
        //查询条件构造器 给查询带上条件，查parent_id=0的语句
        QueryWrapper<Category> wq = new QueryWrapper<>();
        wq.eq("parent_id",id);
        return categoryService.list(wq);
    }
}
